<?php

namespace App\Console\Commands;

use App\Models\FlowParkRecord;
use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Models\ParkInfo;
use App\Models\ParkRecord;
use App\Models\Report;

class ChargeCount extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'charge:count';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '收入日统计';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $start       = Carbon::yesterday()->timestamp;
        $end         = Carbon::today()->timestamp;
         //$start       = mktime(0, 0, 0, 7, 3, 2019);
         //$end         = mktime(0, 0, 0, 7, 4, 2019);
        $report_date = date('Y-m-d', $start);

        $parks = ParkInfo::whereIn('park_code', config('custom.report_parks'))
            ->select(['park_code', 'parking_name'])
            ->orderBy('sort_by', 'desc')
            ->get();

        $taxi_park_code = config('custom.taxi_parks');
        $data = $parks->map(function ($item, $key) use ($start, $end, $taxi_park_code) {
            //生成报表列
            $rowKey = 'time_' . $item->park_code;
            $row = [
                $rowKey . '_0_15_little' => 0,
                $rowKey . '_15_30_little' => 0,
                $rowKey . '_0_15_big' => 0,
                $rowKey . '_15_30_big' => 0,
                $rowKey . '_little' => 0,
                $rowKey . '_big' => 0,
                $rowKey . '_total' => 0,
                $rowKey . '_greater_1440_little' => 0,
                $rowKey . '_greater_1440_big' => 0
            ];

            for ($i = 30; $i < 1440; $i += 30) {
                $endTIme = $i + 30;
                $row[$rowKey . "_{$i}_{$endTIme}_little"] = 0;
                $row[$rowKey . "_{$i}_{$endTIme}_big"] = 0;
            }


            //查询前一天数据
            $table = 't_park_record_' . $item->park_code;
            $parkRecord = new ParkRecord();
            $parkRecord->setTable($table);
            if(!in_array($item->park_code, $taxi_park_code)) {
                $res = $parkRecord
                    ->where('out_time', '>=', $start)
                    ->where('out_time', '<', $end)
                    ->select(['parking_duration', 'parking_charge', 'vpl_color', 'charge_items'])->get();
            } else {
                $res = $parkRecord
                    ->where('out_time', '>=', $start)
                    ->where('out_time', '<', $end)
                    ->where('parking_charge', '<>', 500)
                    ->select(['parking_duration', 'parking_charge', 'vpl_color', 'charge_items'])->get();
            }

            foreach ($res as $value) {
                //定位时间范围
                $filed = $this->initFiled($value, $rowKey);
                $calc_charge = 0;
                if ($value->charge_items != '[]' && $value->charge_items != '') {
                    $chargeItems = json_decode($value->charge_items, true);
                    if(is_array($chargeItems)){
                        foreach ($chargeItems as $v) {
                           if(!in_array($v['charge_type'],[1,2,4,48,49,3,50,61])) continue;
                           if ($v['shishou'] > 0) {
                              $calc_charge += $v['shishou'];
                           }
                        }
                    } 
                }
//                $calc_charge = $value->parking_charge / 100;
                $calc_charge = $calc_charge / 100;
                //大车或者小车
                if ($value->vpl_color == 1) {
                    $row[$filed . '_big'] += $calc_charge;
                    $row[$rowKey . '_big'] += $calc_charge;
                } else {
                    $row[$rowKey . '_little'] += $calc_charge;
                    $row[$filed . '_little'] += $calc_charge;
                }

                $row[$rowKey . '_total'] += $calc_charge;
            }
            return $row;
        });

        //存入数据库
        $report = Report::where('type', 'charge_count_day_report')
                            ->where('report_date', $report_date)
                            ->first();
        if (!$report) {
            $report = new Report;
            $report->report_date = $report_date;
            $report->type = 'charge_count_day_report';
        }
        
        $report->value = $data->collapse();
        $report->save();

        // 清除流量数据
        FlowParkRecord::truncate();
        $this->info('收入统计成功');
    }

    /**
     * 计算时间范围
     *
     * @param $value
     * @param $key
     * @return string
     */
    private function initFiled($value, $key)
    {
        $time = ceil($value->parking_duration / 60);
        if ($time > 0 && $time <= 15) return "{$key}_0_15";
        if ($time > 15 && $time <= 30) return "{$key}_15_30";

        for ($i = 30; $i < 1440; $i += 30) {
            if ($time > $i && $time <= $i + 30) return "{$key}_" . $i . "_" . ($i + 30);
        }

        return $key . "_greater_1440";
    }
}
